import { defineStore } from 'pinia'
import { ref } from 'vue';

interface Tab {
  title: string;
  name: string;
}

export const useMeanStore = defineStore('mean', () => {
  // 菜单数据
  const menuList = ref([])
  // 权限数据
  const permList = ref([])
  const hasRoute = ref(false)
  const editableTabsValue = ref('Index')
  const editableTabs = ref([
    {
      title: '首页',
      name: 'Index'
    }
  ])

  function addTabs(tab: Tab) {
    let index = editableTabs.value.findIndex( (item: { name: string; }) => item.name === tab.name)
    if(index === -1) {
      editableTabs.value.push(tab)
    }
    editableTabsValue.value = tab.name
  }
  function setActiveTab(tabName: string) {
    editableTabsValue.value = tabName
  }

  function changeRouteStatus(state: any) {
    hasRoute.value = state
    sessionStorage.setItem("hasRoute", state)
  }
  function setMenuList(menus: any) {
    menuList.value = menus
  }
  function setPermList(authoritys: any) {
    permList.value = authoritys
  }
  return {
    menuList,
    permList,
    hasRoute,
    editableTabsValue,
    editableTabs,
    changeRouteStatus,
    setMenuList,
    setPermList,
    addTabs,
    setActiveTab
  }
})